package inteface.utils;

import inteface.ArbrePanel;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.print.*;
import javax.print.attribute.*;

/**
*  Print2DPrinterJob
* 
*  @author Vincent GUIJARRO
*  @version 0.2, 29/03/06
*/
public class Print2DPrinterJob {
		//private Graphics g;
	
        public Print2DPrinterJob(ArbrePanel a, File f) {
        	//this.g = g;
        	 /* Create a print job */
        	/*PrinterJob job = PrinterJob.getPrinterJob();
        	job.setPrintable(a);
        	String psMimeType = "application/postscript";
        	FileOutputStream fos; 
        	StreamPrintService psPrinter = null; 
        	StreamPrintServiceFactory []factories =
        	        PrinterJob.lookupStreamPrintServices(psMimeType);
        	if (factories.length > 0) {
        		System.out.println("ici");
        	        try { 
        	                fos = new FileOutputStream(new File("out.ps")); 
        	                psPrinter = factories[0].getPrintService(fos); 
        	                // psPrinter can now be set as the service on a PrinterJob 
        	        } catch (FileNotFoundException e) { } 
        	} 
        	try {
        		job.setPrintService(psPrinter); // if app wants to specify this printer.
            	PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
            	aset.add(OrientationRequested.LANDSCAPE);
        		job.print(aset);
        	}
        	catch (PrinterException e) {
        		System.err.println(e);
        	}
        	} */
        	/* Use the pre-defined flavor for a Printable from an InputStream */
            DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;

            /* Specify the type of the output stream */
            String psMimeType = DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType();

            /* Locate factory which can export a GIF image stream as Postscript */
            StreamPrintServiceFactory[] factories =
            StreamPrintServiceFactory.lookupStreamPrintServiceFactories(
                                    flavor, psMimeType);
            if (factories.length == 0) {
                    System.err.println("No suitable factories");
                    System.exit(0);
            }

            try {
                    /* Create a file for the exported postscript */
                    FileOutputStream fos = new FileOutputStream(f);

                    /* Create a Stream printer for Postscript */
                    StreamPrintService sps = factories[0].getPrintService(fos);
                    //System.out.println(factories[0].getOutputFormat());
                    /* Create and call a Print Job */
                    DocPrintJob pj = sps.createPrintJob();
                    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
                    //aset.add(new DocumentName(f2.getName(), Locale.FRANCE));
                    
                    /*Attribute tab[] = sps.getAttributes().toArray();
                    for (int i = 0; i < tab.length; i++)
                    	System.out.println(tab[i]);*/

                    Doc doc = new SimpleDoc(a, flavor, null);

                    pj.print(doc, aset);
                    fos.close();

            } catch (PrintException pe) { 
                    System.err.println(pe);
            } catch (IOException ie) { 
                    System.err.println(ie);
            }
        }
        /*public int print(Graphics g, PageFormat pf,int pageIndex) {

                if (pageIndex == 0) {
                        Graphics2D g2d= (Graphics2D)g;
                        g2d.translate(pf.getImageableX(), pf.getImageableY()); 
                        g2d.setColor(Color.black);
                        g2d.drawString("example string", 250, 250);
                        g2d.fillRect(0, 0, 200, 200);
                        return Printable.PAGE_EXISTS;                                   
                } else {
                        return Printable.NO_SUCH_PAGE;
                }
        }*/
}
